<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');
session_start();

class sms extends CI_Controller {

    function __construct() {
        parent::__construct();
    }

    function inbox() {
        if ($this->session->userdata('logged_in')) {
            $session_data = $this->session->userdata('logged_in');
            $data['username'] = $session_data['username'];
            $this->load->model('m_sms');
            $this->load->model('mModel');

            $config['base_url'] = base_url() . "sms/inbox";
            $config['total_rows'] = $this->m_sms->jumlahbaris_i();
            $config['per_page'] = 3;
            $config['first_link'] = 'Awal';
            $config['last_link'] = 'Terakhir';
            $config['cur_page'] = 1;
            $config['full_tag_open'] = "<div class='pagination'><ul>";
            $config['full_tag_close'] = '</ul></div>';
            $config['last_tag_open'] = '<li>';
            $config['last_tag_close'] = '</li>';
            $config['first_tag_open'] = '<li>';
            $config['first_tag_close'] = '</li>';
            $config['next_tag_open'] = '<li>';
            $config['next_tag_close'] = '</li>';
            $config['prev_tag_open'] = '<li>';
            $config['prev_tag_close'] = '</li>';
            $config['cur_tag_open'] = "<li class='active'><span>";
            $config['cur_tag_close'] = "</span></li>";
            $config['num_tag_open'] = '<li>';
            $config['num_tag_close'] = '</li>';
            $this->pagination->initialize($config);
            $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
            $data['table'] = $this->m_sms->inbox_page($config["per_page"], $page);
            $data['links'] = $this->pagination->create_links();

            $data['masuk'] = $this->m_sms->inbox();
            $data['telpon'] = $this->mModel->telpon();
            $data['konten'] = 'inbox';
            $this->load->view('home', $data);
        } else {
            //If no session, redirect to login page
            redirect('login', 'refresh');
        }
    }

    function write() {
        if ($this->session->userdata('logged_in')) {
            $session_data = $this->session->userdata('logged_in');
            $data['username'] = $session_data['username'];
            $this->load->model('mModel');
            $data ['telpon'] = $this->mModel->telpon();
            $data['isi'] = $this->mModel->getUser();
            $data['judul'] = 'Tulis SMS';
            $data['konten'] = 'write';
            $this->load->view('home', $data);
        } else {
            //If no session, redirect to login page
            redirect('login', 'refresh');
        }
    }

    function swrite() {
        if ($this->session->userdata('logged_in')) {
            $session_data = $this->session->userdata('logged_in');
            $data['username'] = $session_data['username'];
            $this->load->model('m_sms');
            $this->load->model('mModel');
            $this->m_sms->write();
            $data ['telpon'] = $this->mModel->telpon();
            $data['isi'] = $this->mModel->getUser();
            $data['konten'] = 'notifikasi';
            $data['notifikasi'] = "Pesan berhasil Dikirim";
            $data['Judul'] = "Insert Data Berhasil";
            $this->load->view('home', $data);
        } else {
            //If no session, redirect to login page
            redirect('login', 'refresh');
        }
    }

    function sent() {
        if ($this->session->userdata('logged_in')) {
            $session_data = $this->session->userdata('logged_in');
            $data['username'] = $session_data['username'];
            $this->load->model('m_sms');

            $config['base_url'] = base_url() . "sms/sent";
            $config['total_rows'] = $this->m_sms->jumlahbaris_o();
            $config['per_page'] = 3;
            $config['first_link'] = 'Awal';
            $config['last_link'] = 'Terakhir';
            $config['cur_page'] = 1;
            $config['full_tag_open'] = "<div class='pagination'><ul>";
            $config['full_tag_close'] = '</ul></div>';
            $config['last_tag_open'] = '<li>';
            $config['last_tag_close'] = '</li>';
            $config['first_tag_open'] = '<li>';
            $config['first_tag_close'] = '</li>';
            $config['next_tag_open'] = '<li>';
            $config['next_tag_close'] = '</li>';
            $config['prev_tag_open'] = '<li>';
            $config['prev_tag_close'] = '</li>';
            $config['cur_tag_open'] = "<li class='active'><span>";
            $config['cur_tag_close'] = "</span></li>";
            $config['num_tag_open'] = '<li>';
            $config['num_tag_close'] = '</li>';
            $this->pagination->initialize($config);
            $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
            $data['table'] = $this->m_sms->outbox_page($config["per_page"], $page);
            $data['links'] = $this->pagination->create_links();

            $data['terkirim'] = $this->m_sms->sent();
            $data['konten'] = 'sent';
            $this->load->view('home', $data);
        } else {
            //If no session, redirect to login page
            redirect('login', 'refresh');
        }
    }

    function balas($no) {
        //error_reporting(0);
        if ($this->session->userdata('logged_in')) {
            $session_data = $this->session->userdata('logged_in');
            $data['username'] = $session_data['username'];
            $this->load->model('m_sms');
            $this->load->model('mModel');
            $data ['telpon'] = $this->mModel->telpon();
            $data['isi'] = $this->mModel->getUser();
            $data['balas'] = $this->m_sms->balas($no);
            $data['konten'] = 'reply';
            $this->load->view('home', $data);
        } else {
            //If no session, redirect to login page
            redirect('login', 'refresh');
        }
    }

    function sbalas() {
        if ($this->session->userdata('logged_in')) {
            $session_data = $this->session->userdata('logged_in');
            $data['username'] = $session_data['username'];
            $this->load->model('m_sms');
            $this->load->model('mModel');
            $this->m_sms->reply();
            $data ['telpon'] = $this->mModel->telpon();
            $data['isi'] = $this->mModel->getUser();
            $data['konten'] = 'notifikasi';
            $data['notifikasi'] = "Pesan berhasil Dikirim";
            $data['Judul'] = "Insert Data Berhasil";
            $this->load->view('home', $data);
        } else {
            //If no session, redirect to login page
            redirect('login', 'refresh');
        }
    }

    function logout() {
        $this->session->unset_userdata('logged_in');
        session_destroy();
        redirect('home', 'refresh');
    }

}
